package com.example.userservice.services;

import com.example.userservice.feignclient.G2FeignClient;
import com.example.userservice.feignclient.UserFeignClient;
import com.example.userservice.servicedomain.UserDomain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.*;

import static com.example.userservice.util.Util.delay;
import static java.util.stream.Collectors.toList;

@Service
public class UserService {
    @Autowired
    private UserFeignClient userFeignClient;

    @Autowired
    private G2FeignClient g2FeignClient;


    //say1
    public String sayHi(String name){
        delay();
        return "a";
        //return userFeignClient.sayHi(name);
    }

    public String testZipKin(HashMap map){
         g2FeignClient.testZipKin(map);
        return "";
    }

    public String goon(String json){
        return userFeignClient.sayHi(json);
    }



    //异步任务
    public Future<String> asyncSayHi(String name){
        return CompletableFuture.supplyAsync(()->sayHi(name));
    }


    public String getResult(String name) throws ExecutionException, InterruptedException {
        String price = asyncSayHi(name).get();
        return price;
    }

    //什么叫异步
    public String aSayHi(String name) throws ExecutionException, InterruptedException {
        CompletableFuture.supplyAsync(()->sayHi(name));
        String price = asyncSayHi(name).get();
        return price;
    }

    public String otherFunSayHai(String name){
        List<UserDomain> userDomains = Arrays.asList(new UserDomain(),new UserDomain(),new UserDomain(),new UserDomain());
        long startTime = System.currentTimeMillis();
        List<String> str = userDomains.stream().map(user -> user.getHai("z")).collect(toList());
        System.err.println("domain耗时：" + (System.currentTimeMillis() - startTime));
        return null;
    }


}
